package com.augurit.common.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.augurit.common.base.dao.BaseDao;
import com.augurit.common.entity.AcUserRole;
import com.augurit.common.entity.OmUser;
import com.augurit.common.system.convert.AcRoleConverter;
import com.augurit.common.system.convert.OmUserConverter;
import com.augurit.common.system.web.org.form.AcUserRoleForm;
import com.augurit.common.system.web.org.form.OmUserForm;
import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.lang.CollectionUtils;


/**
 * 用户角色关联DAO类
 */
@Repository
public class AcUserRoleDao extends BaseDao<AcUserRole, Long> {

	/**
	 * 保存用户角色关联Form对象
	 * @param form 用户角色关联Form对象
	 */
	public void save(AcUserRoleForm form){
		
		if(form != null){
			AcUserRole entity = null;
			
			//准备VO对象
			if(form != null && form.getUrId() != null){
				entity = this.get(form.getUrId());
			}else{
				entity = new AcUserRole();
			}
			
			//属性值转换
			AcRoleConverter.convertToEntity(form, entity);
			
			//保存
			super.save(entity);
			
			//回填ID属性值
			form.setUrId(entity.getUrId());
		}
	}
	
	/**
	 * 删除指定角色关联的用户
	 * @param roleId 角色id
	 */
	public void deleteByRoleId(Long roleId){
		if(roleId == null)
			return;
		
		List<AcUserRole> entities = this.findBy("roleId",roleId);
		if(entities != null && entities.size() > 0){
			this.delete(entities);
		}
	}
	
	/**
	 * 查询指定用户id并满足指定角色的记录
	 * @param userId
	 * @param roleId
	 * @return 
	 */
	public List<AcUserRole> getByUserIdAndRoleId(Long userId,Long roleId){
		if(userId != null && roleId != null){
			String hql = "from AcUserRole ps where ps.userId = ? and ps.roleId = ?";
			List values = new ArrayList();
			values.add(userId);
			values.add(roleId);
			
			return this.find(hql, values);
		}
		return null;
	}
	
	/**
	 * 查询指定用户id并满足指定角色的记录
	 * @param userId
	 * @param roleId
	 * @return 
	 */
	public Long[] getUrIdsByUserIdAndRoleId(Long userId,Long roleId){
		if(userId != null && roleId != null){
			String hql = "select ps.urId from AcUserRole ps where ps.userId = ? and ps.roleId = ?";
			List values = new ArrayList();
			values.add(userId);
			values.add(roleId);
			
			List<Long> list = this.find(hql, values);
			return CollectionUtils.toLongArray(list);
		}
		return null;
	}
	/**
	 * 分配指定角色给指定的用户
	 * @param roleId
	 * @param userId
	 */
	public void assignRoleToUser(Long roleId, Long userId){
		if(roleId != null && userId != null){
			Long[] ids = this.getUrIdsByUserIdAndRoleId(userId, roleId);
			//如果指定用户不存在指定角色，则给它分配此角色
			if(ids == null){
				AcUserRole entity = new AcUserRole();
				entity.setRoleId(roleId);
				entity.setUserId(userId);
				this.save(entity); 
			}
		}
	}
	
	/**
	 * 使指定用户不具有指定角色
	 * @param roleId
	 * @param userId
	 */
	public void disAssociateRoleFromUser(Long roleId,Long userId){
		if(roleId != null && userId != null){
			Long[] urIds = getUrIdsByUserIdAndRoleId(userId,roleId);
			if(urIds != null && urIds.length > 0)
				this.delete(urIds);
		}
		
	}
	
	/**
	 * 使指定的用户列表不具有指定角色
	 * @param roleId
	 * @param userIds
	 */
	public void disAssociateRoleFromUsers(Long roleId,Long[] userIds){
		if(roleId != null && userIds != null && userIds.length > 0){
			String hql = "from AcUserRole ps where ps.roleId = ? and ";
			String condition = HqlUtils.buildOrCondition("ps", "userId", userIds);
			hql += condition;
			List values = new ArrayList();
			values.add(roleId);
			List <AcUserRole> entities = this.find(hql, values);
			if(entities != null && entities.size() > 0)
				this.delete(entities);
		}
	}

	/**
	 * 找出用户直接拥有的角色
	 * @param userId 用户id
	 * @return 角色列表
	 */
	public List<AcUserRole> getDirectRolesByUserId(Long userId){
		return partSelectRolesByUserId(userId, null);
	}
	
	public List<Long> getDirectRoleIdsByUserId(Long userId){
		return partSelectRolesByUserId(userId, "roleId");
	}
	
	/**
	 * 获取指定应用的模块并控制返回要查询的结果
	 * @param userId 用户ID
	 * @param entityProperty 返回要查询的结果。例如：要返回Entity对象的moduleId属性，则设为“moduleId”。
	 * @return
	 */
	private List partSelectRolesByUserId(Long userId, String entityProperty){
		String hql = "select " 
						+ (entityProperty != null && entityProperty.trim().length() > 0 ? "ps." + entityProperty : "ps") 
						+ " from AcUserRole ps where ps.userId = ?";
		List values = new ArrayList();
		values.add(userId);
		
		return this.find(hql,values);
	}
	
	
	/**
	 * 找出不拥有指定角色的用户列表
	 * @param roleId 用户id
	 * @return
	 */
	public List<OmUserForm> getUsersNotAssociateRole(Long roleId){
		if(roleId != null){
			String hql = "select os from OmUser os where os.userId not in " +
					     "( select ps.userId from AcUserRole ps where ps.roleId = ? )";
			List values = new ArrayList();
			values.add(roleId);
			List<OmUser> list = this.find(hql, values);
			return OmUserConverter.convertToFormList(list);
		}
		return null;
	}
	
	/**
	 * 找出拥有指定角色的所有用户id列表
	 * @param roleId
	 * @return
	 */
	public List<Long> getUserIdsByRole(Long roleId){
		if(roleId != null){
			String hql = "select ps.userId from AcUserRole ps where ps.roleId = ?";
			List values = new ArrayList();
			values.add(roleId);
			return this.find(hql, values);
		}
		return null;
	}
}
